const OSS = require('ali-oss');
const STS = OSS.STS;

class OSSModel {
    constructor(bucket="tuguaishou-pic"){
        let bucket_config = {
            "ali_access_key_id": global.BASE_CONFIG["ali_access_key_id"],
            "ali_key_secret": global.BASE_CONFIG["ali_key_secret"]
        };

        if(bucket == "tuguaishou-download"){
            bucket_config = {
                "ali_access_key_id": global.BASE_CONFIG["ali_bucket_tuguaishou-download"]["ali_access_key_id"],
                "ali_key_secret": global.BASE_CONFIG["ali_bucket_tuguaishou-download"]["ali_key_secret"]
            };
        }

        this.oss_client = "";
        this.ossClient_sts = "";
        this.ali_access_key_id = bucket_config["ali_access_key_id"];
        this.ali_key_secret = bucket_config["ali_key_secret"];
        this.ali_region = global.BASE_CONFIG["ali_region"];
        this.ali_role_arn = global.BASE_CONFIG["ali_role_arn"];
        this.ali_session_name = global.BASE_CONFIG["ali_session_name"];
        this.ali_endpoint = global.BASE_CONFIG["ali_endpoint"] ? global.BASE_CONFIG["ali_endpoint"] : "oss-cn-shanghai-internal.aliyuncs.com";
        this.sts_token = "";
        this.bucket = bucket;
    }


    connection(){
        let connection_config = {
            region: this.ali_region,
            accessKeyId: this.ali_access_key_id,
            accessKeySecret: this.ali_key_secret,
            bucket: this.bucket,
            endpoint: this.ali_endpoint,
        };
        try{
            this.oss_client = new OSS(connection_config);
        }catch (e) {
            console.log(e);
            return false;
        }
        return true;
    }


    async connectionSTS(){
        let sts = new STS({
            accessKeyId: this.ali_access_key_id,
            accessKeySecret: this.ali_key_secret
        });
        let policy = {
            "Statement": [
                {
                "Action": [
                    "oss:Get*"
                ],
                "Effect": "Allow",
                "Resource": ["acs:oss:*:*:" + this.bucket + "/*"]
                }
            ],
            "Version": "1"
        };
        try{
            let token = await sts.assumeRole(this.ali_role_arn, policy, 15 * 60, this.ali_session_name);
            this.sts_token = token;
            this.ossClient_sts = new OSS({
                region: this.ali_region,
                accessKeyId: token.credentials.AccessKeyId,
                accessKeySecret: token.credentials.AccessKeySecret,
                stsToken: token.credentials.SecurityToken,
                bucket: this.bucket,
                endpoint: this.ali_endpoint,
                // endpoint: "oss-cn-shanghai.aliyuncs.com"
                // endpoint: "oss-cn-shanghai-internal.aliyuncs.com"
            });
        }catch (e) {
            console.log(e);
            return false;
        }

        return true;
    }

    getSaltImgUrlOSS(img_file, expires, options = {}){
        let url = "";
        try{
            url = this.ossClient_sts.signatureUrl(img_file, {
                expires: expires,
                process: options.urlProcess
            });
        }catch (e) {
            console.log(e);
            return false;
        }

        return url;
    }


    async uploadFile(cdnFile, localFile){
        let result = ""
        try{
            result = await this.oss_client.put(cdnFile, localFile);
        }catch (e) {
            console.log(e);
            return false;
        }
        return result;
    }
}

module.exports = {OSSModel};